package com.hslxy.niukewang.controller;

import com.hslxy.niukewang.entity.Comment;
import com.hslxy.niukewang.entity.DiscussPost;
import com.hslxy.niukewang.entity.Event;
import com.hslxy.niukewang.entity.User;
import com.hslxy.niukewang.event.EventProducer;
import com.hslxy.niukewang.service.CommentService;
import com.hslxy.niukewang.service.DiscussPostService;
import com.hslxy.niukewang.util.Constant;
import com.hslxy.niukewang.util.HostHolder;
import com.hslxy.niukewang.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

@Controller
@RequestMapping("/comment")
public class CommentController implements Constant {

    @Autowired
    private CommentService commentService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){
        User user = hostHolder.getUsers();
        comment.setUserId(user.getId());
        comment.setCreateTime(new Date());
        comment.setStatus(0);
        commentService.addComment(comment);
        //        系统通知触发事件
        Event event = new Event().setTopic(TOPIC_COMMENT)
                .setEntityId(comment.getEntityId())
                .setUserId(user.getId())
                .setEntityType(comment.getEntityType())
                .setData("postId", discussPostId);
        if (comment.getEntityType() == ENTITY_TYPE_POST){
            DiscussPost discussPost = discussPostService.findDiscussPost(comment.getEntityId());
            event.setEntityUserId(discussPost.getUserId());
        }else if(comment.getEntityType() == ENTITY_TYPE_COMMENT){
            Comment targetcomment = commentService.findCommentById(comment.getEntityId());
            event.setEntityUserId(targetcomment.getUserId());
        }
        eventProducer.fireEvent(event);
//        搜索引擎触发事件
        if(comment.getEntityType() == ENTITY_TYPE_POST){
            event = new Event()
                    .setTopic(TOPIC_PUBLISH)
                    .setUserId(comment.getUserId())
                    .setEntityType(ENTITY_TYPE_POST)
                    .setEntityId(discussPostId);
            eventProducer.fireEvent(event);
            //        计算帖子分数
            String postScoreKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(postScoreKey, discussPostId);
        }

        return "redirect:/discuss/detail/" + discussPostId;
    }


}
